home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS01.ADF / C / FixDate.c < prev    next >
C/C++ Source or Header  |  1986-01-09  |  8KB  |  326 lines

  1. /*
  2.  *    FIXDATE.C Version 0.0     December 12, 1985
  3.  *    Copyright (c) 1985 Lee B. Grey, Atlanta, GA
  4.  *
  5.  * Permission is hereby granted to distribute FIXDATE freely, as long as
  6.  * no money is exchanged.  It is in the public domain.  No copies should
  7.  * be distributed without these comments or the credits.
  8.  *
  9.  * Written by Lee B. Grey
  10.  *            250 Bruton Way
  11.  *            Atlanta, GA  30342
  12.  *            (404) 851-9103
  13.  *
  14.  * FIXDATE works in the same manner as TREE, and they should be
  15.  * distributed together.  FIXDATE is an ad hoc fix to a weird Amiga
  16.  * "virus."  Due to some oddities in the way the Amiga's DATE command
  17.  * works and the way the Amiga checks dates on disks, many disks now
  18.  * have corrupted dates, which are virtually impossible to get rid of
  19.  * manually.  Before you know it, your Amiga is in the 650th century.
  20.  * And Amigas do not work well that far in the future.
  21.  *
  22.  * FIXDATE checks every file and every directory on the specified disk,
  23.  * and corrects any dates which are not within the user's  specified
  24.  * limits.
  25.  *
  26.  * Like TREE, FIXDATE is sloppy, uncommented, and not something I am eager
  27.  * to have people associate with my talents.  However, it works, and it is
  28.  * needed.  In addition to the updates I would like to make to TREE, FIXDATE
  29.  * could stand some specific improvements of its own:
  30.  *
  31.  *   @  FIXDATE should accept a date, rather than the number of days as an
  32.  *      integer.  Of course, to avoid the problems with DATE, FIXDATE
  33.  *      should receive a four-digit year, not a two-digit year.
  34.  *
  35.  *   @  FIXDATE will probably need to work on hard-disks and other floppies.
  36.  *      It currently only knows about df0: and df1:.
  37.  *
  38.  *   @  FIXDATE should fix the system date, as well.  At the moment, you can
  39.  *      fix all of the dates on a disk and then promptly screw it up all
  40.  *      over again, since the system date was not reset.  To avoid this,
  41.  *      after fixing the dates on a disk, that disk should be removed.
  42.  *      Another alternative is to reset the machine after both disks have
  43.  *      been FIXDATEd.
  44.  *
  45.  */
  46. #include <exec/types.h>
  47. #include <exec/io.h>
  48. #include <devices/trackdisk.h>
  49.  
  50. #define TD_READ CMD_READ
  51. #define TD_WRITE CMD_WRITE
  52. #define BLOCKSIZE TD_SECTOR
  53. #define FL_SIZE sizeof(FileList)
  54.  
  55. typedef struct FileListStruct{
  56.    LONG  block;
  57.    struct FileListStruct *next;
  58. } FileList;
  59.  
  60. LONG LastLegal,ChangeTo;
  61. char *malloc();
  62. BYTE diskbuffer[BLOCKSIZE];
  63. struct Port *diskport;
  64. struct IOStdReq *diskreq;
  65. FileList *Top,*Bot;
  66.  
  67.  
  68. instruct()
  69. {
  70.    printf("\n\nUsage:  FIXDATE <drive> <last-legal-date> <set-bad-dates-to>\n\n");
  71.    printf("  Drive:               ( df0: | df1: )\n");
  72.    printf("  Last legal date:     Any date later than this date will be fixed\n");
  73.    printf("  Set bad dates to:    Any date that is fixed will be set to this date\n");
  74.    printf("\n                       These dates must be entered as an integer\n");
  75.    printf("                       which is the number of days since 01-JAN-78.\n");
  76.    printf("\n                       As a reference, 10-DEC-1985 was day 2900.\n\n");
  77.    exit(1);
  78. }
  79.  
  80.  
  81. main(argc,argv)
  82. int argc;
  83. char **argv;
  84. {
  85.    SHORT error;
  86.    LONG drivenum,atol();
  87.    extern struct Port *CreatePort();
  88.    extern struct IOStdReq *CreateStdIO();
  89.    FileList *tmp;
  90.  
  91.    if(argc != 4)
  92.       instruct();
  93.    else
  94.       printf("\n   FIXDATE Version 0.0   Copyright 1985 Lee B. Grey\n\n");
  95.    if(!strcmp(argv[1],"df0:"))
  96.       drivenum = 0L;
  97.    else
  98.       if(!strcmp(argv[1],"df1:"))
  99.          drivenum = 1L;
  100.       else
  101.          instruct();
  102.    LastLegal = atol(argv[2]);
  103.    ChangeTo = atol(argv[3]);
  104.    diskport = CreatePort(0,0);
  105.    diskreq = CreateStdIO(diskport);
  106.    error = OpenDevice(TD_NAME,drivenum,diskreq,0);
  107.    if(error){
  108.       printf("Error opening device.\n\n");
  109.       endtree();
  110.    }
  111.    Bot = Top = (FileList *)malloc(FL_SIZE);
  112.    if(Bot == NULL){
  113.       printf("No memory left.\n\n");
  114.       endtree();
  115.    }
  116.    Top->block = 880;
  117.    Top->next = NULL;
  118.    while(Top != NULL){
  119.       ReadBlock(Top->block);
  120.       if(diskreq->io_Error != 0){
  121.          printf("Error in main after ReadBlock.\n\n");
  122.          endtree();
  123.       }
  124.       printf("\n\nDirectory %s",&diskbuffer[433]);
  125.       FixDate(Top->block);
  126.       ScanHash();
  127.       tmp = Top;
  128.       Top = Top->next;
  129.       if(free( (char *)tmp ) != 0){
  130.          printf("Error in freeing memory.\n\n");
  131.          endtree();
  132.       }
  133.       ShowFiles();
  134.    }
  135.    endtree();
  136. }
  137.  
  138.  
  139. FixChkSum()
  140. {
  141.    LONG *data,tot;
  142.    int i;
  143.  
  144.    data = &diskbuffer[20];
  145.    tot = *data = 0L;
  146.    data = &diskbuffer[0];
  147.    for(i=0;i<128;i++)
  148.       tot = tot + *(data++);
  149.    data = &diskbuffer[20];
  150.    *data = ~tot + 1L;
  151. }
  152.  
  153.  
  154. FixDate(blk)
  155. LONG  blk;
  156. {
  157.    LONG *date1,*date2,*date3;
  158.  
  159.    date1 = &diskbuffer[420];
  160.    date2 = &diskbuffer[472];
  161.    date3 = &diskbuffer[484];
  162.    if(*date1 > LastLegal ||
  163.       (blk == 880 && *date2 > LastLegal) ||
  164.       (blk == 880 && *date3 > LastLegal)   ){
  165.       if(*date1 > LastLegal)
  166.          *date1 = ChangeTo;
  167.       if(blk == 880 && *date2 > LastLegal)
  168.          *date2 = ChangeTo;
  169.       if(blk == 880 && *date3 > LastLegal)
  170.          *date3 = ChangeTo;
  171.       FixChkSum();
  172.       WriteBlock(blk);
  173.       printf("             date set to %ld\n",ChangeTo);
  174.    }else
  175.       printf("\n");
  176. }
  177.  
  178.  
  179. ShowFiles()
  180. {
  181.    FileList *use,*prev,*tmp;
  182.    LONG *diskdata;
  183.  
  184.    use = Top;
  185.    prev = Top = Bot = NULL;
  186.    while(use != NULL){
  187.       ReadBlock(use->block);
  188.       if(diskreq->io_Error != 0){
  189.          printf("I/O error in ShowFiles.\n");
  190.          endtree();
  191.       }
  192.       diskdata = &diskbuffer[0];
  193.       if(*diskdata != 2L){
  194.          printf("Weird block read.\n\n");
  195.          use = use->next;
  196.          continue;
  197.       }
  198.       diskdata = &diskbuffer[496];
  199.       if(*diskdata != 0L){
  200.          tmp = (FileList *)malloc(FL_SIZE);
  201.          if(tmp == NULL){
  202.             printf("HASHCHAIN: No memory left.\n\n");
  203.             endtree();
  204.          }
  205.          tmp->block = *diskdata;
  206.          tmp->next = use->next;
  207.          use->next = tmp;
  208.       }
  209.       diskdata = &diskbuffer[508];
  210.       if(*diskdata == 2L)
  211.          if(Top == NULL){
  212.             Bot = prev = Top = use;
  213.             use = use->next;
  214.             continue;
  215.          }else{
  216.             Bot = prev = use;
  217.             use = use->next;
  218.             continue;
  219.          }
  220.       if(*diskdata == -3L){
  221.          printf("   %s",&diskbuffer[433]);
  222.          FixDate(use->block);
  223.          if(prev == NULL){
  224.             tmp = use;
  225.             use = use->next;
  226.             if(free( (char *)tmp ) != 0){
  227.                printf("SHOWFILES: Error in deallocation of memory.\n\n");
  228.                endtree();
  229.             }
  230.             continue;
  231.          }else{
  232.             tmp = use;
  233.             use = use->next;
  234.             prev->next = use;
  235.             if(free( (char *)tmp ) != 0){
  236.                printf("SHOWFILES: Error in deallocation of memory.\n\n");
  237.                endtree();
  238.             }
  239.             continue;
  240.          }
  241.       }
  242.       printf("Unrecognized code = %ld  block = %ld\n\n",
  243.                *diskdata,use->block);
  244.       prev = use;
  245.       use = use->next;
  246.    }
  247. }
  248.  
  249.  
  250. ScanHash()
  251. {
  252.    SHORT i;
  253.    LONG *data;
  254.    FileList *new;
  255.  
  256.    for(data = &diskbuffer[24],i=6;i<78;i++,data++){
  257.       if(*data == 0L)
  258.          continue;
  259.       new = (FileList *)malloc(FL_SIZE);
  260.       if(new == NULL){
  261.          printf("SCANHASH: No memory left.\n\n");
  262.          endtree();
  263.       }
  264.       new->block = *data;
  265.       new->next = NULL;
  266.       Bot->next = new;
  267.       Bot = new;
  268.    }
  269. }
  270.  
  271.  
  272. WriteBlock(blk)
  273. LONG blk;
  274. {
  275.    diskreq->io_Length = BLOCKSIZE;
  276.    diskreq->io_Data = diskbuffer;
  277.    diskreq->io_Command = TD_WRITE;
  278.    diskreq->io_Offset = blk*512;
  279.    DoIO(diskreq);
  280.    if(diskreq->io_Error != 0){
  281.       printf("Error in write.\n\n");
  282.       endtree();
  283.    }
  284. }
  285.  
  286.  
  287. ReadBlock(blk)
  288. LONG blk;
  289. {
  290.    diskreq->io_Length = BLOCKSIZE;
  291.    diskreq->io_Data = diskbuffer;
  292.    diskreq->io_Command = TD_READ;
  293.    diskreq->io_Offset = blk*512;
  294.    DoIO(diskreq);
  295.    if(diskreq->io_Error != 0){
  296.       printf("Error in read.\n\n");
  297.       endtree();
  298.    }
  299. }
  300.  
  301.  
  302. MotorOff()
  303. {
  304.    diskreq->io_Length = 0;
  305.    diskreq->io_Command = TD_MOTOR;
  306.    DoIO(diskreq);
  307.    return;
  308. }
  309.  
  310.  
  311. endtree()
  312. {
  313.    FileList *tmp;
  314.  
  315.    MotorOff(diskreq);
  316.    CloseDevice(diskreq);
  317.    DeleteStdIO(diskreq);
  318.    DeletePort(diskport);
  319.    while(Top != NULL){
  320.       tmp = Top;
  321.       Top = Top->next;
  322.       if(free( (char *)tmp ) != 0)
  323.          printf("Error in deallocation of memory.\n\n");
  324.    }
  325. }
  326.